package com.tan.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    //查询数据库所有信息
    //没有实体类，数据库中的东西，怎么获取？Map
    @GetMapping("/userList")
    public List<Map<String,Object>>  userList()
    {
        String sql = "select * from user";
        List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql);
        return mapList;
    }
    @GetMapping("/addUser")
    public String addUser()
    {
        String sql="insert into user values (5,'小名','3141213')";
        jdbcTemplate.update(sql);
        return "update-ok";
    }
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id")int id)
    {
        String sql="update user set name=?,pwd=? where id="+id;

        //封装
        Object[] objects = new Object[2];
        objects[0] = "小明";
        objects[1] = "313413";
        jdbcTemplate.update(sql,objects);
        return "update-ok";
    }
    @GetMapping("/delUser/{id}")
    public String delUser(@PathVariable("id")int id)
    {
        String sql="delete from user where id=?";
        jdbcTemplate.update(sql,id);
        return "delUser-ok";
    }
}
